import { RouteRecordRaw, useRoute } from "vue-router";
let firstRoute:any= null
export function routesMapToMenu(userMenus: any[]) {
  const routes: RouteRecordRaw[] = []
  const allRoutes:RouteRecordRaw[] = []
  const routeFiles = import.meta.globEager('../router/main/*/*/*.ts')
  Object.keys(routeFiles).forEach(item => {
    allRoutes.push(routeFiles[item].default);
  })
  const _recurseGetRoute = (menus: any[]) => {
    for (const menu of menus) { 
      if (menu.type === 2) {
        const route = allRoutes.find(route => route.path === menu.url)
        if (route) {
          routes.push(route)
        }
        if (!firstRoute) {
          firstRoute = menu
        }
      } else {
        _recurseGetRoute(menu.children)
      } 
    }
  }
  _recurseGetRoute(userMenus)
  
  return routes
}
export function routeMapToBreadcrumb(userMenus:any[],currentPath:string) {
  ///main/analysis/overview curentPath
  const paths = currentPath.split('/')
  const type1Path = `/${paths[1]}/${paths[2]}`
  
  const breadcrumbList:any[] = []
  for (const menu of userMenus) {
    if (menu.url === type1Path) {
      breadcrumbList.push(menu)
      for (const submenu of menu.children) {
        if (submenu.url === currentPath) {
          breadcrumbList.push(submenu)
          break
        }
      }
      break
    }
  }
  return breadcrumbList
}
export {
  firstRoute
}